unit TJ.FMX.SgcForward;

interface

uses
  System.SyncObjs,
  qjson,
  qstring,
  TJ.FMX.FileOper,
  TJ.FMX.Comm,
  TJ.FMX.SecondComm,
  TJ.FMX.ControlComm,
  //IdCustomHTTPServer,sgcWebSocket_Classes_Indy,
  //sgcBase_Classes, sgcSocket_Classes, sgcTCP_Classes,
  //sgcWebSocket_Classes,
  sgcIdContext,sgcIdCustomHTTPServer,
  sgcBase_Classes, sgcSocket_Classes,
  sgcTCP_Classes, sgcWebSocket_Classes, sgcWebSocket_Server, sgcWebSocket,
  sgcWebSocket_Classes_Indy,
  System.SysUtils, System.Classes;

type

  THxSgcforward = class(THxLog)
  private
    FHttpServer :  TsgcWebSocketHTTPServer;
    FDataJson   :  TQJson;
    FFileName   :  string;
    procedure BeforeForwardHTTP(Connection: TsgcWSConnection;ARequestInfo: TIdHTTPRequestInfo; aForward: TsgcWSServerForwardHTTP);
    procedure CommandGet(AContext: TIdContext; ARequestInfo:TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
    function  GetUrlAddr(Fun:string): string;
  public
    procedure Stop;
    procedure Start;

    constructor Create(Port: Integer;FileName:string;Log:TOnLogEvent);
    destructor Destroy; override;
  end;

implementation

{ THxSgcforward }

constructor THxSgcforward.Create(Port: Integer;FileName:string; Log: TOnLogEvent);
begin
  SetLog(Log);
  FDataJson :=  TQJson.Create;
  FFileName :=  FileName;
  FDataJson.LoadFromFile(FFileName);
  FHttpServer := TsgcWebSocketHTTPServer.Create(nil);
  FHttpServer.Port :=  Port;
  FHttpServer.OnBeforeForwardHTTP :=  BeforeForwardHTTP;
  FHttpServer.OnCommandGet :=  CommandGet;
  LogOut('Create success');
end;

function THxSgcforward.GetUrlAddr(Fun:string): string;
var
  ListStr   :     TStringList;
begin
  ListStr :=  TStringList.Create;
  ListStr.Delimiter := '/';
  ListStr.DelimitedText := Fun;
  if ListStr.Count < 2 then
    Result := ''
  else
    Result := FDataJson.ValueByName(ListStr.Strings[2],'');
  FreeAndNil(ListStr);
end;

procedure THxSgcforward.BeforeForwardHTTP(Connection: TsgcWSConnection;
    ARequestInfo: TIdHTTPRequestInfo; aForward: TsgcWSServerForwardHTTP);
var
  CurStr    :     string;
begin
  CurStr := GetUrlAddr(ARequestInfo.URI);
  if CurStr.Length = 0 then
  begin
    if ARequestInfo.URI = '/app/UseOper' then
    begin
      FDataJson.LoadFromFile(FFileName);
      LogOut('BeforeForwardHTTP-reload-'+FDataJson.Encode(False));
    end
    else
      LogOut('BeforeForwardHTTP-error-'+ARequestInfo.URI);
  end
  else
  begin
    aForward.Enabled := True;
    aForward.URL :=  StringReplace(CurStr,'https://','http://',[rfReplaceAll]);
    LogOut('BeforeForwardHTTP-'+ARequestInfo.URI+'-'+aForward.URL);
  end;
end;

procedure THxSgcforward.CommandGet(AContext: TIdContext; ARequestInfo:
    TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
  AResponseInfo.ContentText := CreatJsonRet(239,'not found the url.');
  AResponseInfo.ContentType := 'application/json';
  AResponseInfo.ResponseNo := 200;
end;

procedure THxSgcforward.Start;
begin
  LogOut('Start.');
  FHttpServer.Start;
end;

procedure THxSgcforward.Stop;
begin
  LogOut('THxSgcforward Stop.');
  FHttpServer.Stop;
  FHttpServer.Active := False;
end;

destructor THxSgcforward.Destroy;
begin
  Stop;
  FreeAndNil(FHttpServer);
  inherited;
end;

end.
